package com.etrans.web.gps.business.base.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.base.domain.PubOrganzation;
import com.etrans.web.gps.business.base.domain.PubOrganzationvo;
import com.etrans.web.gps.business.base.domain.Unit;
import com.etrans.web.gps.business.base.service.PubOrganzationService;
import com.etrans.web.gps.business.log.domain.OperateRecord;
import com.etrans.web.gps.business.log.service.OperationRecordService;
import com.etrans.web.gps.common.page.PageVo;
import com.etrans.web.gps.util.TreeCommon;
import com.etrans.web.gps.util.grid.FlexiGridUtil;

@Controller
@RequestMapping("/base/PubOrganzation/")
public class PubOrganzationControcller extends BaseController{
	
	@Autowired
	private OperationRecordService  operationRecordService;
	@Autowired
	private PubOrganzationService pubOrganzationService;
	
	
	@RequestMapping(value="toPubOrganzation.html")
	public  String  toPubOrganzation(){
		OperateRecord log = new OperateRecord("运政机构管理","运政机构管理列表查看",this.getSysUser());
		operationRecordService.insertlog(log);
		return "views/business/base/organzation/puborganzation";
	}
	
	@RequestMapping(value="toaddPubOrganzation.html")
	public String toaddPubOrganzation(Model modle,String id){
		if(id!=null&&!"".equals(id)){
			PubOrganzationvo vo =pubOrganzationService.queryOneById(Integer.parseInt(id));
			modle.addAttribute("vo",vo);
			modle.addAttribute("action","update");
		}else{
			modle.addAttribute("action","save");
		}
		return "views/business/base/organzation/addpuborganzation";
	}
	
	@RequestMapping(value="addPubOrganzation.html")
	public @ResponseBody Map<String,Object> addPubOrganzation(PubOrganzationvo vo){
		PubOrganzation org = new PubOrganzation();
		org.setContacts(vo.getContacts());
		org.setContactsAddress(vo.getContactsAddress());
		org.setContactsNumber(vo.getContactsNumber());
		org.setCreateTime(new Date());
		org.setMemo(vo.getMemo());
		org.setOrgnCode(vo.getOrgnCode());
		org.setOrgnName(vo.getOrgnName());
		org.setParentid(vo.getParentid());
		org.setEmail(vo.getEmail());
		org.setEnable_email(vo.getEnable_email());
		org.setEnable_sms(vo.getEnable_sms());
		org.setParent_orgn(vo.getParent_orgn());
		if(vo.getArea_id()!=null&&!"".equals(vo.getArea_id())){
			org.setArea_id(Integer.parseInt(vo.getArea_id()));
		}
		org.setArea_name(vo.getArea_name());
		Map< String,Object> map = new HashMap<String, Object>();
		if(pubOrganzationService.checkOrgnName("orgnName",vo.getOrgnName())){
			map.put("code",2);
			map.put("msg","机构名称已存在");
			return map;
		}
		if(pubOrganzationService.checkOrgnName("orgnCode",vo.getOrgnCode())){
			map.put("code",3);
			map.put("msg","机构编码已存在");
			return map;
		}
		if(pubOrganzationService.addPubOrganzation(org)){
			map.put("code", 0);
			map.put("msg","添加成功！");
		}else {
			map.put("code", 1);
			map.put("msg","添加失败，系统繁忙！");
		}
		return map;
	}
	
	
	@RequestMapping("queryPubOrganzation.html")
	public @ResponseBody  PageVo  queryPubOrganzation(String paramsGrid){
		Map<String,Object> map = FlexiGridUtil.parseJSONParam(paramsGrid).getWhereParam();
		map.put("startpage",((int)map.get("page")-1)*((int)map.get("pageSize")));
		map.put("limit",(int)map.get("pageSize"));
		return pubOrganzationService.getPubOrganzation(map);
	}
	
	@RequestMapping("queryFullTree.html")
	@SuppressWarnings("rawtypes")
	public @ResponseBody List queryFullTree() {
		@SuppressWarnings("unused")
		List<Unit> rows = null;
		List<Map<String, Object>> unitTree = null;
		try {
			// 参数赋值
			HashMap<String, Object> paramsMap = new HashMap<String, Object>();
			// 先查出所有的企业信息，按照fullID排序
			List<HashMap<String, Object>> listTreeList = pubOrganzationService.queryFullUnitTree( paramsMap);
			// 获取顶级企业的长度
			int FirUnitlevel = 0;
			if (listTreeList.size() > 0) {
				FirUnitlevel = listTreeList.get(0).get("FullID").toString().length();
			}
			// 创建树
			unitTree = TreeCommon.createTree(listTreeList, FirUnitlevel);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return unitTree;
	}
	
	@RequestMapping(value="updatePubOrganzation.html")
	public @ResponseBody Map<String,Object> updatePubOrganzation(PubOrganzationvo vo){
		PubOrganzation org = new PubOrganzation();
		org.setId(vo.getId());
		org.setContacts(vo.getContacts());
		org.setContactsAddress(vo.getContactsAddress());
		org.setContactsNumber(vo.getContactsNumber());
		org.setParentid(vo.getParentid());
		org.setMemo(vo.getMemo());
		org.setOrgnCode(vo.getOrgnCode());
		org.setOrgnName(vo.getOrgnName());
		org.setParent_orgn(vo.getParent_orgn());
		org.setEmail(vo.getEmail());
		org.setEnable_sms(vo.getEnable_sms());
		org.setEnable_email(vo.getEnable_email());
		if(vo.getArea_id()!=null&&!"".equals(vo.getArea_id())){
			org.setArea_id(Integer.parseInt(vo.getArea_id()));
		}
		org.setArea_name(vo.getArea_name());
		
		Map< String,Object> map = new HashMap<String, Object>();
		if(pubOrganzationService.updatePubOrganzation(org)){
			map.put("code", 0);
		}else {
			map.put("code", 1);
		};
		return map;
	}
	
	@RequestMapping("deleteOrganzation.html")
	public @ResponseBody Map<String,Object> deleteOrganzation(String id){
		Map<String,Object> map =new HashMap<String,Object>();
		if(id!=null&&!"".equals(id)){
			String[] sid = id.split(",");
			List<Integer> list = new ArrayList<Integer>();
			for (String str : sid) {
				list.add(Integer.parseInt(str));
			}
			if(pubOrganzationService.deleteOrganzation(list)){
				map.put("code", 0);
			}else {
				map.put("code", 1);
			};
		}else {
			map.put("code", 1);
		}
		return map;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "exportOrganzation.html")
	public void exportRecord( HttpServletRequest request, HttpServletResponse response,@RequestBody String paramsGrid, String colModel, String title, String StartPage, String EndPage, String pagesize) {
		Map<String, Object> paramMap=getReqParameters();
		List<String> colModelList = JSON.parseObject(colModel, List.class);
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("startpage",Integer.parseInt(StartPage)-1);
		param.put("limit",(Integer.parseInt(EndPage) - Integer.parseInt(StartPage)+1)*Integer.parseInt(pagesize));
		paramMap.putAll(param);
		PageVo pageVo = pubOrganzationService.getPubOrganzation(paramMap);
		List<PubOrganzationvo> list = (List<PubOrganzationvo>)pageVo.getRows();
		List<HashMap<String, Object>> rows = new ArrayList<HashMap<String, Object>>();
		int start=Integer.valueOf(Integer.valueOf(StartPage)-1)*Integer.valueOf(pagesize);
		int end=Integer.valueOf(EndPage)*Integer.valueOf(pagesize);
		if(end>(list.size()-1)){
			end=list.size();
		}
		for(int i=start;i<end;i++){
			HashMap<String, Object> map = new HashMap<String, Object>();
			PubOrganzationvo obj=(PubOrganzationvo)list.get(i);
			map.put("orgnCode", obj.getOrgnCode()==null ? "" : obj.getOrgnCode());
			map.put("orgnName", obj.getOrgnName()==null ? "" : obj.getOrgnName());
			map.put("area_name", obj.getArea_name()==null ? "" : obj.getArea_name());
			map.put("createTime", obj.getCreateTime()==null ? "" : obj.getCreateTime());
			map.put("parent_orgn", obj.getParent_orgn()==null ? "" : obj.getParent_orgn());
			map.put("contacts", obj.getContacts()==null ? "" : obj.getContacts());
			map.put("contactsNumber", obj.getContactsNumber()==null ? "" : obj.getContactsNumber());
			map.put("email", obj.getEmail()==null ? "" : obj.getEmail());
			map.put("contactsAddress", obj.getContactsAddress()==null ? "" : obj.getContactsAddress());
			map.put("enable_email", obj.getEnable_email()==1 ? "启用" :"停用");
			map.put("enable_sms", obj.getEnable_sms()==1 ? "启用" :"停用");
			map.put("memo", obj.getMemo()==null ? "" : obj.getMemo());
			rows.add(map);
		}
		int index=-1;
		@SuppressWarnings("rawtypes")
		List rowsList = new ArrayList<>();
		rowsList.add(rows);
		exportExl(request, response,title, removeHeadName(colModelList,index,-1),rowsList,null);
	}
	
}
